package dev.ece.suit.heightmap;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.RigidBody;

import dev.ece.core.shader.DepthShader;
import dev.ece.core.sprite.Sprite;
import dev.ece.core.texture.Texture;
import dev.ece.suit.defer.DeferCommonShader;
import dev.ece.util.jbullet.EceJbulletWorld;
import dev.ece.util.tools.Camera;

/**
 * @author 潜行的青衣
 * @see
 */
public class HeightMapSprite extends Sprite<Object, HeightMapModel> {

	private Texture texture;
	
	private DeferCommonShader deferCommonShader = DeferCommonShader.getDefaultDeferCommonShader();
	
	private DepthShader depthShader = DepthShader.getDepthShader();
	
	private RigidBody rigidBody;
	
	/**
	 * @param model
	 */
	public HeightMapSprite(HeightMapModel model, Texture texture) {
		super(model);
		this.texture = texture;
	}

	@Override
	public void onRenderDepthMap(Camera camera) {
		depthShader.glUseProgram();
		depthShader.glModelViewProjectMatrix();
		super.onRenderDepthMap(camera);
	}

	@Override
	public void onRender(Camera camera) {
		this.deferCommonShader.glUseProgram();
		this.deferCommonShader.glModelViewProjectMatrix();
		this.deferCommonShader.glMaterial(this.getModel().getMaterial());
		this.deferCommonShader.glTexture(texture);
		this.deferCommonShader.glDecalable(true);
		super.onRender(camera);
	}

	public RigidBody addToEceJbulletWorld(EceJbulletWorld world) {
		removeFromEceJbulletWorld(world);
		CollisionShape shape = this.getModel().createCollisionShape();
		rigidBody = world.addRigidBody(shape, 0);
		rigidBody.setUserPointer(this);
		return rigidBody;
	}
	
	public void removeFromEceJbulletWorld(EceJbulletWorld world) {
		if(rigidBody != null) {
			world.removeRigidBody(rigidBody);
			rigidBody = null;
		}
	}
	
}
